package com.atguigu.tingshu.user.pattern.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.pattern.DeliveryStrategy;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Slf4j
@Component(SystemConstant.ORDER_ITEM_TYPE_VIP)
public class VIPDelivery  implements DeliveryStrategy {


    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void delivery(UserPaidRecordVo userPaidRecordVo) {

        //根据type来判断用户买的是那种商品,是VIP还是专辑,还是声音

        Long userId = userPaidRecordVo.getUserId();

        String itemType = userPaidRecordVo.getItemType();

        String orderNo = userPaidRecordVo.getOrderNo();

        List<Long> itemIdList = userPaidRecordVo.getItemIdList();

        //走到这说明买的是VIP
        //这个功能比较复杂
        //1.首先需要根据订单编号查询是否处理过订单
        Long count = userVipServiceMapper.selectCount(
                new LambdaQueryWrapper<UserVipService>()
                        .eq(UserVipService::getOrderNo, orderNo)
                        .select(UserVipService::getId)
        );

        if (count>0){
            return;
        }

        //2.然后需要新增会员购买记录
        UserVipService userVipService = new UserVipService();
        userVipService.setUserId(userId);
        userVipService.setOrderNo(orderNo);

        //获取用户身份信息,判断是否为VIP
        UserInfo userInfo = userInfoMapper.selectById(userId);

        Integer voIsVip = userInfo.getIsVip();

        Date vipExpireTime = userInfo.getVipExpireTime();

        boolean isVip = false;
        if (voIsVip == 1 && vipExpireTime.after(new Date())){
            //是VIP
            isVip = true;
        }


        //获取用户购买的套餐信息,得到套餐服务
        Long vipId = itemIdList.get(0);

        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(vipId);

        Integer serviceMonth = vipServiceConfig.getServiceMonth();

        if (!isVip){
            //普通用户
            //生效时间为当前时间
            userVipService.setStartTime(new Date());

            //到期时间
            userVipService.setExpireTime(DateUtil.offsetMonth(new Date(),serviceMonth));

        }else {
            //会员VIP
            //本次会员的生效时间为会员到期时间+1天
            DateTime dateTime = DateUtil.offsetDay(userInfo.getVipExpireTime(), 1);
            userVipService.setStartTime(dateTime);

            //本次会员的到期时间
            DateTime offsetMonth = DateUtil.offsetMonth(userVipService.getStartTime(), serviceMonth);
            userVipService.setExpireTime(offsetMonth);
        }

        userVipServiceMapper.insert(userVipService);

        //3更新用户会员标识以及更新会员过期时间
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(userVipService.getExpireTime());
        userInfoMapper.updateById(userInfo);
    }
}
